/**
 * Created by Sihan on 2017/5/15.
 */

/**
 * 全局提示框
 * 依赖bootstarp jquery bootstrap-notify animate.css
 * @param {string} msg 
 * @param {string} warn 
 */
function Alert(msg, warn) {
    var options = {
        icon: 'glyphicon glyphicon-info-sign',
        message: msg
    };

    var settings = {
        type: "info",
        animate: {
            enter: 'animated fadeInDown',
            exit: 'animated fadeOutUp'
        },
        delay: 5000,
        z_index: 9999
    };

    if (warn == 'success') {
        options['icon'] = 'glyphicon glyphicon-ok-sign';
    }
    else if (warn == 'warning') {
        options['icon'] = 'glyphicon glyphicon-warning-sign';
    }
    else if(warn == 'danger') {
        options['icon'] = 'glyphicon glyphicon-remove-sign';
    }

    if (typeof (warn) == 'string'){
        settings['type'] = warn;
    }
    $.notify(options, settings);
}

/**
 * Ajax request info
 * @param {string} url 
 * @param {string: 'post', 'get'} method 
 * @param {json} data 
 * @param {string} tipElement 
 * @param {function} callback 
 */
function requestData(url, method, data, tipElement, callback) {
    //Spinner 配置项
    var opts = {
        lines: 7 // The number of lines to draw
        , length: 0 // The length of each line
        , width: 20 // The line thickness
        , radius: 20 // The radius of the inner circle
        , scale: 0.25 // Scales overall size of the spinner
        , corners: 1 // Corner roundness (0..1)
        , color: '#333' // #rgb or #rrggbb or array of colors
        , opacity: 0.25 // Opacity of the lines
        , rotate: 0 // The rotation offset
        , direction: 1 // 1: clockwise, -1: counterclockwise
        , speed: 1 // Rounds per second
        , trail: 10 // Afterglow percentage
        , fps: 20 // Frames per second when using setTimeout() as a fallback for CSS
        , zIndex: 2e9 // The z-index (defaults to 2000000000)
        , className: 'spinner' // The CSS class to assign to the spinner
        , top: 'auto' // Top position relative to parent
        , left: '50%' // Left position relative to parent
        , shadow: false // Whether to render a shadow
        , hwaccel: true // Whether to use hardware acceleration
    };
    var ajaxTime = null;
    var tip = $("#" + tipElement);
    var spinner = null;
    var msg = null;
    var reData = null;

    if (typeof (tip) == 'object') {
        spinner = new Spinner(opts);
        spinner.spin(tip.get(0));
    }

    ajaxTime = setTimeout(function(){
        if (typeof (spinner) == 'object') {
            spinner.stop();
        }
        msg = "服务器通讯中断，请稍候再试";
        Alert(msg, "warning");
        if (typeof (callback) === 'function') {
            callback('failure', null);
        }
    }, 5000);

    var procAjax = function (res) {
        //request response stop spinner
        if (typeof (spinner) == 'object') {
            spinner.stop();
        }
        //clearTimeout function
        if (ajaxTime) {
            clearTimeout(ajaxTime);
            ajaxTime = null;
        }
        try {
            reData = $.parseJSON(res);
            if (parseInt(reData.err) === 0) {
                if (typeof (callback) === 'function') {
                    callback('success', reData);
                }
            } else {
                Alert(reData.msg, "danger");
                if (typeof (callback) === 'function') {
                    callback('failure', null);
                }
            }
        }catch(e){
            Alert(res, "danger");
            if (typeof (callback) === 'function') {
                callback('failure', null);
            }
        }
    };

    if (method == 'post') {
        $.post(url, data, procAjax); 
    } else {
        $.get(url, data, procAjax);
    }
}

/**
 * post form
 * @param {string} formId 
 * @param {string} url 
 * @param {string} tipId 
 * @param {list} btnList 
 * @param {function} callback 
 */
function postForm(formId, url, tipId, btnList, callback) {
    var form = $("#" + formId);
    if (typeof (form) != 'object') {
        Alert("不能获取表单信息", "danger");
        return;
    }
    //disable button
    for (i in btnList) {
        $("#" + btnList[i]).attr("disabled", true);
    }
    //serialize data json
    var data = form.serializeArray();
    //add isAjax post
    data[data.length] = { name: "isAjax", value: 1 };
    requestData(url, 'post', data, tipId, function (stat, msg) {
        if (stat == 'failure') {
            for (i in btnList) {
                $("#" + btnList[i]).attr("disabled", false);
            }
            return;
        }
        if (typeof (callback) === 'function') {
            callback(msg);
        }
    });
}

$(function(){
    $("#_task").click(function(){
        requestData('/task', 'get', {"isAjax": 1}, 'task-progess', function (state, data){
            if(state === 'success') {
                $("#task-title").html(data.time);
                if(parseInt(data.progress) == 100) {
                    $("#" + 'task-progess').html(data.info);
                }else{
                    $("#" + 'task-progess').html(data.info + data.progress + '%');
                }
                //$(".progress-bar").attr("data-transitiongoal", data.progress);
                $(".progress-bar").css("width", data.progress + "%");
            }
        });
    });
});